package com.baseframe.tool.utils.reptile;

import com.baseframe.tool.utils.data.StringUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ReptileUtil {

    public static Document buildHtml(String htmlStr){

        return Jsoup.parse(htmlStr);
    }

    public static String getFirstImage(String htmlStr){

        if(StringUtil.isBlank(htmlStr)){
            return null;
        }

        Document document = buildHtml(htmlStr);

        Elements imgageElements = document.getElementsByTag("img");

        if (imgageElements.size()>0){
            Element element = imgageElements.get(0);

            return element.attributes().get("src");
        }

        return null;
    }

    public static void main(String[] args) {
        String htmlStr = "<p>关于测试竞赛.....</p><p><strong>一、竞赛内容</strong></p><p style=\"text-indent: 2em;\">关于测试竞赛的相关内容啊啊按计划尽快大家来看大量看大家来看大量进口来得及啊离开家记得啦空间丽江大理看见立刻记录大家来看就了空间打开链接了。</p><p style=\"text-indent: 2em;\">酒啊就开了就看了大家来看就看了大家来看尽量靠近大陆空军考虑就立刻大家来看看见了大家来就看了大家就凯迪拉克就了解到了卡简历库就立刻大家来看。</p><pre><code class=\"language-java\">public void main(String[] args){\n" +
                "     System.out.println(\"Hello world\");   \n" +
                "}\n" +
                "</code></pre><p>竞赛图片</p><p><img src=\"https://bofeng-1253372311.cos.ap-beijing.myqcloud.com/upload/20230629/c1d6abe61136762673a51d13b8c7d02f.png\" alt=\"c1d6abe61136762673a51d13b8c7d02f.png\" data-href=\"https://bofeng-1253372311.cos.ap-beijing.myqcloud.com/upload/20230629/c1d6abe61136762673a51d13b8c7d02f.png\" style=\"width: 608.40px;height: 377.07px;\"/></p><p>竞赛视频</p><p><br></p><div data-w-e-type=\"video\" data-w-e-is-void>\n" +
                "<video poster=\"\" controls=\"true\" width=\"800\" height=\"auto\"><source src=\"https://wxjsz.oss-cn-beijing.aliyuncs.com/52551378264886567421566639999207.mp4\" type=\"video/mp4\"/></video>\n" +
                "</div><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p>";
        String htmlStr2 = "<p>222222</p>";
        String firstImage = getFirstImage(htmlStr2);

        System.out.println(firstImage);
    }

}
